using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using IQIGame.Onigao.GameAOT;
using System;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_LevelMapScaleSlider : UIBaseNode
    {
        #region Template Generate,don't modify

        protected class UIB_UIN_LevelMapScaleSlider
        {
            public ExText scale { protected set; get; }
            public ExSlider slider { protected set; get; }
            public ExButton btnMinus { protected set; get; }
            public ExButton btnAdd { protected set; get; }

            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<ExText>("scale", out var __tbv0);
                scale = __tbv0;
                __binding.TryGetVariableValue<ExSlider>("slider", out var __tbv1);
                slider = __tbv1;
                __binding.TryGetVariableValue<ExButton>("btnMinus", out var __tbv2);
                btnMinus = __tbv2;
                __binding.TryGetVariableValue<ExButton>("btnAdd", out var __tbv3);
                btnAdd = __tbv3;
            }
        }

        #endregion Template Generate,don't modify


        #region fields

        private Action<float> _valueChangeCallBack;

        private static float _initialScale = 150f;

        #endregion fields

        #region properties

        public event Action<float> OnSliderValueChange
        {
            add => _valueChangeCallBack += value;
            remove => _valueChangeCallBack -= value;
        }

        protected UIB_UIN_LevelMapScaleSlider ui { get; set; }
        protected UIMsgDispatcher uiMsgDispatcher => _msgDispatcher as UIMsgDispatcher;

        #endregion properties

        protected override void BeforeInit()
        {
            ui = new UIB_UIN_LevelMapScaleSlider();
            ui.InitBinding(csObjBind);
        }

        protected override void OnInit()
        {
            ui.slider.minValue = 100f;
            ui.slider.maxValue = 200f;
        }

        protected override void OnShow()
        {
            var initialScale = _initialScale;
            ui.slider.value = initialScale;
            ui.scale.text = ToPercent(initialScale);
        }

        protected override void OnHide()
        {

        }

        protected override void AddUGUIListener()
        {
            ui.slider.onValueChanged.AddListener(OnSliderValueChanged);
            ui.btnMinus.AddClickEvent(OnBtnMinusClick);
            ui.btnAdd.AddClickEvent(OnBtnAddClick);
        }

        protected override void OnDispose()
        {
        }

        #region methonds

        public void SetValue(float value)
        {
            ui.slider.value = value;
        }

        private void OnSliderValueChanged(float value)
        {
            ui.scale.text = ToPercent(value);
            _valueChangeCallBack?.Invoke(value * 0.01f);
            _initialScale = value;
        }

        private string ToPercent(float value)
        {
            return Mathf.Round(value) + "%";
        }

        private void OnBtnAddClick()
        {
            ui.slider.value += 10f;
        }

        private void OnBtnMinusClick()
        {
            ui.slider.value -= 10f;
        }

        #endregion methonds
    }
}